package com.ylq.controller;

import com.ylq.entity.Result;
import com.ylq.entity.RoleModules;
import com.ylq.service.RoleModulesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author qiang
 * @Description TODO
 * @Date 2020-05-02
 **/
@RestController
@Api(description = "【权限资源】相关操作")
@RequestMapping("/api")
public class RoleModulesController {
    @Autowired
    private RoleModulesService rmService;

    @ApiOperation("设置权限(保存指定的角色能够访问的所有功能模块到系统中)")
    @PostMapping("SetSysRights")
    public Object setSysRights(HttpSession session,@ApiParam("数据库密钥")@RequestParam("token") String token,@ApiParam("功能模块id拼接的字符串(以逗号分隔)")@RequestParam("parentIds") String  parentIds,@ApiParam("要设置的角色的id")@RequestParam("rId") String rId){
        String getToken = (String) session.getAttribute("token");
        if(getToken.equals(token)){
        //删除
        rmService.deletePid(rId);
        int i= rmService.setSysRights(parentIds,rId);
        if (i>0){
            return new Result("","设置成功",true);
        }else{
            return new Result("","设置失败",false);
        }

        }else {
            return new Result("", "数据库密钥不匹配", false);
        }
    }
    @ApiOperation("获取角色权限数据(获取指定角色能够访问的功能模块数据)")
    @PostMapping("GetModulesByRoleIdLayUI")
    public Object getModulesByRoleIdLayUI(HttpSession session, @ApiParam("数据库密钥")@RequestParam("token") String token, @ApiParam("角色的id")@RequestParam("roleId") String roleId){
        String getToken = (String) session.getAttribute("token");
        if(getToken.equals(token)){
        return rmService.turnTree(roleId);

        }else {
            return new Result("", "数据库密钥不匹配", false);
        }
    }
}
